package com.huihui.activity.setting;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huihui.R;
import com.huihui.base.BaseActivity;
import com.huihui.datamanager.AppManager;
import com.huihui.utils.IntentUtils;

/**
 * 设置
 */
public class SettingActivity extends BaseActivity implements View.OnClickListener{

    private TextView logout;
    private RelativeLayout psw_definy;
    private RelativeLayout user_setting;
    private RelativeLayout bind_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
//        getSupportActionBar().setTitle(R.string.my_setting);
        titleView.setText(R.string.my_setting);
        initView();
        initListener();
    }

    private void initListener() {
        logout.setOnClickListener(this);
        psw_definy.setOnClickListener(this);
        user_setting.setOnClickListener(this);
        bind_phone.setOnClickListener(this);
    }

    private void initView() {
        logout = (TextView) findViewById(R.id.logout);
        psw_definy = (RelativeLayout) findViewById(R.id.psw_definy);
        user_setting = (RelativeLayout) findViewById(R.id.user_setting);
        bind_phone = (RelativeLayout) findViewById(R.id.bind_phone);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.logout:
                logout();
                break;
            case R.id.psw_definy:
                IntentUtils.gotoSettingDefinePswActivity(getContext());
                break;
            case R.id.user_setting:
                IntentUtils.gotoMineSettingActivity(getContext());
                break;
            case R.id.bind_phone:
                if(AppManager.getUserInfo() != null){
                    if(TextUtils.isEmpty(AppManager.getUserInfo().getTelPhone())){
                        IntentUtils.gotoSettingBindPhoneActivity(getContext());
                    }else{
                        Dialog dialog;
                        DialogInterface.OnClickListener dialogOnclicListener = new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                switch (which) {
                                    case Dialog.BUTTON_POSITIVE:
                                        IntentUtils.gotoSettingBindPhoneActivity(getContext());
                                        dialog.dismiss();
                                        break;
                                    case Dialog.BUTTON_NEGATIVE:
                                        dialog.dismiss();
                                        break;
                                }
                            }
                        };

                        AlertDialog.Builder builder = new AlertDialog.Builder(this,3);
                        builder.setMessage("您已经绑定手机号，确定修改吗?");
                        builder.setPositiveButton("确认", dialogOnclicListener);
                        builder.setNegativeButton("取消", dialogOnclicListener);
                        dialog = builder.create();
                        dialog.show();
                    }
                }

                break;
        }
    }


    private void logout() {
        AppManager.setToken(null);
        AppManager.setUserInfo(null);
        System.exit(0);
    }
}
